Transactions

When a connection is created it is in auto commit mode. This means that each individual SQL statement is treated as a transaction will be automatically commited right after it is executed. The way to allow two or more ORACLE statements to be grouped into a transaction is to disable auto commit mode.

Program on Transaction.

import java.sql.*;
public class Sample
{
public static void main(String args[])
{
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vision","scott","tiger");
con.setAutoCommit(false);
PreparedStatement pst=con.prepareStatement(“update emp set sal=?
where eno=?”);
pst.setInt(1,5000);
pst.setInt(2,100);
pst.executeUpdate();
PreparedStatement pst1=con.prepareStatement(“update emp set sal=?
where eno=?”);
pst1.setInt(1,1000);
pst1.setInt(2,200);
pst1.executeUpdate();
con.commit();

}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}